<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Channel_Model extends CI_Model{
	protected $_name = "channel";
	protected $_primary = "ChannelId";
	var $NameChannel_vn = "NameChannel_vn", $NameChannel_en = "NameChannel_en", $CodesChannel = "CodesChannel", $Description_vn = "Description_vn", $Description_en = "Description_en", $Logo = "Logo", $TcId = "TcId", $Order = "`Order`", $Status = "Status";
	
	function codesChannel($add_vaule = null, $sl = 200){
		$array1 = array();
		for($i=1; $i<=$sl; $i++){
			$array1[] = $i;
		}
		$this->db->select($this->CodesChannel);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$j=0;
			foreach($query->result_array() as $row){
				$data[$j] = $row[$this->CodesChannel];
				$j++;
			}
			$result = array_diff($array1, $data);
			if($add_vaule != null){
				array_push($result, $add_vaule);
			}
			array_unique($result);
			sort($result);
			return $result;
		}
		return $array1;
	}
	
	function getMaxOrder(){
		$this->db->select_max($this->Order);
		return $this->db->get($this->_name)->row();
	}
	
	function insertChannel($urlImage){
		$this->db->set($this->NameChannel_vn, trim($this->input->post($this->NameChannel_vn, true)));
		$this->db->set($this->NameChannel_en, trim($this->input->post($this->NameChannel_en, true)));
		$this->db->set($this->CodesChannel, trim($this->input->post($this->CodesChannel, true)));
		$this->db->set($this->Description_vn, trim($this->input->post($this->Description_vn)));
		$this->db->set($this->Description_en, trim($this->input->post($this->Description_en)));
		$this->db->set($this->Logo, trim($urlImage));
		$this->db->set($this->TcId, $this->input->post('typechannel', true));
		$this->db->set($this->Order, $this->getMaxOrder()->Order + 1);
		$this->db->set($this->Status, 1);
		$this->db->insert($this->_name);
	}
	
	function updateChannel($ChannelId, $urlImage){
		$data = array(
					$this->NameChannel_vn => trim($this->input->post($this->NameChannel_vn, true)),
					$this->NameChannel_en => trim($this->input->post($this->NameChannel_en, true)),
					$this->CodesChannel => trim($this->input->post($this->CodesChannel, true)),
					$this->Description_vn => trim($this->input->post($this->Description_vn)),
					$this->Description_en => trim($this->input->post($this->Description_en)),
					$this->Logo => trim($urlImage),
					$this->TcId	=> $this->input->post('typechannel', true),
				);
		$this->db->where($this->_primary, $ChannelId);
		$this->db->update($this->_name, $data);
	}
	
	function countChannel(){
		$this->db->where($this->Status, 1);
		$this->db->or_where($this->Status, 2);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	
	function getAllChannel(){
		$this->db->select($this->_name.'.*, tc.Name_vn as Name_vn');
		$this->db->join("type_channel as tc","tc.TcId=".$this->_name.".".$this->TcId);
		$this->db->where($this->_name.".".$this->Status, 1);
		$this->db->where("tc.".$this->Status, 1);
		$this->db->order_by($this->Order, "ASC");
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function getFirstChannel(){
		$this->db->select($this->_name.'.*, tc.Name_vn as Name_vn');
		$this->db->join("type_channel as tc","tc.TcId=".$this->_name.".".$this->TcId);
		$this->db->where($this->_name.".".$this->Status, 1);
		$this->db->where("tc.".$this->Status, 1);
		$this->db->order_by($this->_primary, "ASC");
		$this->db->limit(1);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			return $query->row();
		}
		return false;
	}
	
	function getChannelLimit($currentPage, $number){
		$this->db->select($this->_name.'.*, tc.Name_vn as Name_vn');
		$this->db->join("type_channel as tc","tc.TcId=".$this->_name.".".$this->TcId);
		$this->db->where("tc.".$this->Status, 1);
		$where = "(".$this->_name.".".$this->Status."=1 OR ".$this->_name.".".$this->Status."=2)";
		$this->db->where($where);
		$this->db->order_by($this->Order, "ASC");
		$this->db->limit($number, $currentPage);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = $currentPage;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function getChannelByTcId($TcId){
		$this->db->select($this->_name.'.*, tc.Name_vn as Name_vn');
		$this->db->join("type_channel as tc","tc.TcId=".$this->_name.".".$this->TcId);
		$this->db->where($this->_name.'.'.$this->TcId, $TcId);
		$this->db->where("tc.".$this->Status, 1);
		$where = "(".$this->_name.".".$this->Status."=1 OR ".$this->_name.".".$this->Status."=2)";
		$this->db->where($where);
		$this->db->order_by($this->Order, "ASC");
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	
	function searchChannel($keysearch){
		$this->db->join("type_channel tc","tc.TcId=".$this->_name.".".$this->TcId);
		$this->db->like($this->NameChannel_vn, $keysearch);
		$this->db->where("tc.".$this->Status, 1);	
		$where = "(".$this->_name.".".$this->Status."=1 OR ".$this->_name.".".$this->Status."=2)";
		$this->db->where($where);
		$this->db->order_by($this->Order, "ASC");
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function changeStatusChannel($ChannelId, $Status){
		try{
			$this->db->where($this->_primary, $ChannelId);
			if($Status == 1){
				$Status = 2;
			}elseif($Status == 2){
				$Status = 1;
			}
			$this->db->update($this->_name, array($this->Status => $Status));
			return $Status;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function getChannelByChannelId($ChannelId){
		$this->db->join("type_channel as tc","tc.TcId=".$this->_name.".".$this->TcId);
		$this->db->where("tc.".$this->Status, 1);
		$this->db->where($this->_primary, $ChannelId);
		$where = "(".$this->_name.".".$this->Status."=1 OR ".$this->_name.".".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	
	function getChannelByOrder($Order){
		$this->db->join("type_channel as tc","tc.TcId=".$this->_name.".".$this->TcId);
		$this->db->where("tc.".$this->Status, 1);
		$this->db->where($this->Order, $Order);
		$where = "(".$this->_name.".".$this->Status."=1 OR ".$this->_name.".".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	
	function deleteChannel($ChannelId){
		try{
			$this->db->where($this->_primary, $ChannelId);
			$this->db->delete($this->_name);
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function updateOrderOfChannel($ChannelId, $Value){
		$data = array(
					$this->Order => $Value
				);
		$this->db->Where($this->_primary, $ChannelId);
		$this->db->update($this->_name, $data);
	}
	
	function moveChannel($ChannelId, $move){
		$channel = $this->getChannelByChannelId($ChannelId);
		if($move == "up"){
			if($channel->Order > 1){
				$channelChange = $this->getChannelByOrder($channel->Order - 1);
			}
		}elseif($move == "down"){
			if($channel->Order < $this->getMaxOrder()->Order){
				$channelChange = $this->getChannelByOrder($channel->Order + 1);
			}
		}	
		if(isset($channelChange)){
			$this->updateOrderOfChannel($channelChange->ChannelId, $channel->Order); 
			$this->updateOrderOfChannel($channel->ChannelId, $channelChange->Order);
			return true; 
		}
		return false;
	
	}
}